package vip.denglei.company.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.base.Splitter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import vip.denglei.company.model.app.AppShoppingCart;
import vip.denglei.company.service.app.AppProductService;
import vip.denglei.company.service.app.AppShoppingCartService;
import vip.denglei.company.util.HttpResult;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 购物车(shopping_cart)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class AppShoppingCartController {
    /**
     * 服务对象
     */
    @Resource
    private AppShoppingCartService shoppingCartService;
    @Resource
    private AppProductService appProductService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("save/shoppingCart")
    public HttpResult saveShoppingCart(@RequestBody AppShoppingCart shoppingCart) {
        log.info("ShoppingCartController saveShoppingCart 参数 shoppingCart:{}", shoppingCart);
        LambdaQueryWrapper<AppShoppingCart> appShoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appShoppingCartLambdaQueryWrapper.eq(AppShoppingCart::getProId, shoppingCart.getProId());
        appShoppingCartLambdaQueryWrapper.eq(AppShoppingCart::getUserId, shoppingCart.getUserId());
        AppShoppingCart appShoppingCart = shoppingCartService.getOne(appShoppingCartLambdaQueryWrapper);
        //如果购物车存在相同产品，直接增加数量
        if (Objects.nonNull(appShoppingCart)) {
            LambdaUpdateWrapper<AppShoppingCart> shoppingCartLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            shoppingCartLambdaUpdateWrapper.set(AppShoppingCart::getProVolume, shoppingCart.getProVolume() + appShoppingCart.getProVolume());
            shoppingCartLambdaUpdateWrapper.eq(AppShoppingCart::getProId, shoppingCart.getProId());
            shoppingCartLambdaUpdateWrapper.eq(AppShoppingCart::getUserId, shoppingCart.getUserId());
            shoppingCartService.update(shoppingCartLambdaUpdateWrapper);
        } else {
            shoppingCartService.save(shoppingCart);
        }
        return HttpResult.success();
    }

    /**
     * 修改购物车数量
     *
     * @param shoppingCart
     * @return
     */
    @PutMapping("update/shoppingCart")
    public HttpResult updateShoppingCart(@RequestBody AppShoppingCart shoppingCart) {
        log.info("ShoppingCartController updateShoppingCart 参数 shoppingCart:{}", shoppingCart);
        if (shoppingCart.getProVolume() <= 0) {
            LambdaQueryWrapper<AppShoppingCart> appShoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
            appShoppingCartLambdaQueryWrapper.eq(AppShoppingCart::getProId, shoppingCart.getProId());
            shoppingCartService.remove(appShoppingCartLambdaQueryWrapper);
        }
        LambdaUpdateWrapper<AppShoppingCart> shoppingCartLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        shoppingCartLambdaUpdateWrapper.set(AppShoppingCart::getProVolume, shoppingCart.getProVolume());
        shoppingCartLambdaUpdateWrapper.eq(AppShoppingCart::getProId, shoppingCart.getProId());
        shoppingCartLambdaUpdateWrapper.eq(AppShoppingCart::getUserId, shoppingCart.getUserId());
        shoppingCartService.update(shoppingCartLambdaUpdateWrapper);
        return HttpResult.success();
    }

    /**
     * 批量删除购物车商品
     *
     * @param ids
     * @return
     */

    @DeleteMapping("delete/shoppingCart")
    public HttpResult deleteShoppingCart(@RequestParam String ids) {
        log.info("ShoppingCartController deleteShoppingCart 参数 ids:{}", ids);
        Splitter split = Splitter.on(',').trimResults().omitEmptyStrings();
        LambdaQueryWrapper<AppShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.in(AppShoppingCart::getId, split.splitToList(ids));
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return HttpResult.success();
    }

    /**
     * 查询个人购物车上平
     *
     * @param userId
     * @return
     */
    @GetMapping("query/shoppingCartByUserId/{userId}")
    public HttpResult queryShoppingCartByUserId(@PathVariable String userId) {
        log.info("ShoppingCartController queryShoppingCartByUserId 参数 userId:{}", userId);
        return HttpResult.success(shoppingCartService.queryShoppingCartByUserId(userId));
    }


}
